//
//  XLWelcomeViewController.swift
//  XLVeibo
//
//  Created by macbook on 16/6/27.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SDWebImage

class XLWelcomeViewController: UIViewController {

    override func loadView() {
        view = bgImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    
    //懒加载初始化控件
    //背景图
    private lazy var bgImageView:UIImageView = UIImageView(imageName: "ad_background")
    //头像
    private lazy var iconImage:UIImageView = {
    
        let img = UIImageView(imageName:"avatar_default_big")
        img.layer.borderColor = ThemeColor.CGColor
        img.layer.borderWidth = 3
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        
        //设置图片
        img.sd_setImageWithURL(NSURL(string:XLUserAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "ad_background"))
        
    return img
    }()
    
    //欢迎回来
    private lazy var messageLabel:UILabel = {
    
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.grayColor()
        label.textAlignment  = .Center
        label.alpha = 0
        
        return label
    }()
    
    
    //更改约束
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        //更改头像约束
        self.iconImage.snp_updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
        }
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: { 
            //如果更改约束farme需要刷新当前新布局
            
            self.view.layoutIfNeeded()
            }) { (_) in
                
              UIView.animateWithDuration(0.25, animations: { 
                
                self.messageLabel.alpha = 1
                 }, completion: { (_) in
                    
                  NSNotificationCenter.defaultCenter().postNotificationName(SwiftRootViewController, object:"welcome")
                    
              })
        }
    }
    
    //设置UI
    private func setupUI(){
        
       view.addSubview(iconImage)
       view.addSubview(messageLabel)
        
       iconImage.snp_makeConstraints { (make) in
        
        make.size.equalTo(CGSize(width: 90, height: 90))
        make.top.equalTo(view).offset(400)
        make.centerX.equalTo(view)

        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconImage.snp_bottom).offset(16)
        }
        
    }
      
}
